package com.xuzhijie.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuzhijie.pms.common.Constants;
import com.xuzhijie.pms.common.Result;
import com.xuzhijie.pms.entity.Feedback;
import com.xuzhijie.pms.entity.MoveRequest;
import com.xuzhijie.pms.exception.PmsServerException;
import com.xuzhijie.pms.mapper.MoveRequestMapper;
import com.xuzhijie.pms.service.IMoveRequestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 许志杰
 * @since 2024-02-17
 */
@Service
public class MoveRequestServiceImpl extends ServiceImpl<MoveRequestMapper, MoveRequest> implements IMoveRequestService {

    @Override
    public Result findPage(Integer page,
                           Integer size,
                           Integer processingStatus,
                           Integer isHaveNumber) {
        Page<MoveRequest> MoveRequestPage = new Page<>(page,size);
        QueryWrapper<MoveRequest> queryWrapper = new QueryWrapper<>();
        Page<MoveRequest> result;
        Long total;

        if (!processingStatus.equals(0)){
            queryWrapper.eq("processing_status",processingStatus);
        }

        if (!isHaveNumber.equals(0)){
            queryWrapper.eq("is_have_number",isHaveNumber);
        }

        try {
            result = page(MoveRequestPage,queryWrapper);
            total = count(queryWrapper);
        }catch (Exception e){
            e.printStackTrace();
            throw new PmsServerException("业务繁忙", Constants.SYSTEM_ERROR);
        }

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("records",result.getRecords());
        resultMap.put("total",total);

        return Result.success(resultMap);
    }

    @Override
    public Result addrepair(String description, String phone, String images, String carNumber, String plateNumber) {
        MoveRequest m = new MoveRequest();
        m.setProcessingStatus(carNumber != null?1:2);
        m.setLicensePlateNumber(plateNumber);
        m.setReason(description);
        m.setSponsor(phone);
        m.setCarNumber(carNumber);
        m.setImg(images);
        save(m);
        return Result.success();
    }
}
